﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Forms;
using System.IO;


namespace vDesktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
           int Y, int cx, int cy, uint uFlags);

        #region Constants
        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;
        const UInt32 SWP_NOACTIVATE = 0x0010;
        const int BALLOON_TIMEOUT_DEFAULT = 3000;

        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
        static readonly IntPtr HWND_TOP = new IntPtr(0);
        #endregion

        NotifyIcon _noteIcon;

        public MainWindow()
        {
            InitializeComponent();
            EstablishNoteIcon();
            SetWindowBottom(this);
        }

        private void EstablishNoteIcon()
        {
            _noteIcon = new System.Windows.Forms.NotifyIcon();
            Stream iconStream = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/vDesktop;component/images/favicon.ico")).Stream;
            _noteIcon.Icon = new System.Drawing.Icon(iconStream);
            _noteIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(_noteIcon_MouseDoubleClick);
            _noteIcon.BalloonTipClicked += new EventHandler(_noteIcon_BalloonTipClicked);
            this.StateChanged += new EventHandler(MainWindow_StateChanged); // Related to non-working event code below
            this.SizeChanged += new SizeChangedEventHandler(MainWindow_SizeChanged);
            _noteIcon.Visible = true;

            // for testing mini behavior. Had trouble trying to minimize it
            this.WindowState = WindowState.Minimized;
        }

        #region Events
        
        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Minimized)
            {
                _noteIcon.BalloonTipTitle = "vDesktop was minimized!";
                _noteIcon.BalloonTipText = "Double-click to show wedge";
                _noteIcon.ShowBalloonTip(BALLOON_TIMEOUT_DEFAULT);
                _noteIcon.Visible = true;
            }
        }
        // This code does not work...
        // you can see what I was hoping to do, but it does not work because
        // this event is never fired if you press "Show Desktop" or
        // [windowkey] + M (Show Desktop)
        // Show desktop is not the same as minimize...I believe here is where
        // we need to make the app or the windows parent the desktop...or something
        void MainWindow_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Minimized)
            {
                _noteIcon.BalloonTipTitle = "vDesktop was minimized!";
                _noteIcon.BalloonTipText = "Double-click to show wedge";
                _noteIcon.Visible = true;
            }
        }

        // This code will block things like windows key minimize and is suppose to block alt F4
        // I think we would have to refine this code though because we probably want to allow other window functions?
        //protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        //{
        //    if (e.Key == Key.System)
        //    {
        //        e.Handled = true;
        //    }

        //    if (e.Key == Key.LWin || e.Key == Key.RWin)
        //    {
        //        e.Handled = true;
        //    }
        //    base.OnKeyDown(e);
        //}


        void _noteIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;
            // Should make window top most
            SetWindowTop(this);
        }

        void _noteIcon_BalloonTipClicked(object sender, EventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;
            // Should make window top most
            SetWindowTop(this);
        }

        #endregion

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            SetWindowBottom(this);
            SetWindowPosition();
        }

        private void SetWindowPosition()
        {
            this.Left = SystemParameters.PrimaryScreenWidth - 20;
            this.Top = 0;
        }

        public static void SetWindowBottom(Window w)
        {
            WindowInteropHelper wih = new WindowInteropHelper(w);
            wih.EnsureHandle();
            IntPtr hWnd = wih.Handle;
            SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
        }

        public static void SetWindowTop(Window w)
        {
            WindowInteropHelper wih = new WindowInteropHelper(w);
            wih.EnsureHandle();
            IntPtr hWnd = wih.Handle;
            SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
        }

        private void btnDesktop_Click(object sender, RoutedEventArgs e)
        {
            SetWindowBottom(this); // Should remove this at some point
        }
    }
}
